package com.opennetexam.manager.mapper;

import com.opennetexam.manager.databasemodel.ExamRoom;
import com.opennetexam.manager.sqlprovider.ExamRoomMapperProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author naicha
 * Created on 2017/11/17.
 */
@Mapper
public interface ExamRoomMapper {

    @Select("SELECT * FROM ExamRoom WHERE AppID = #{appID} AND ExamRoomID = #{examRoomID} LIMIT 1")
    ExamRoom getExamRoomBySomeWhere(@Param(value = "appID") String appID, @Param(value = "examRoomID") String examRoomID);

    @Select("SELECT * FROM ExamRoom WHERE ExamRoomID = #{examRoomId}")
    List<ExamRoom> findExamRoomByRoomId(@Param(value = "examRoomId") String examRoomId);

    @Select("SELECT * FROM ExamRoom WHERE AppID = #{appID} AND ExamSiteID = #{examSiteID} AND ExamRoomID = #{examRoomID} LIMIT 1")
    ExamRoom getExamRoomByAppIdSiteIdRoomId(@Param(value = "appID") String appID, @Param(value = "examSiteID") String examSiteID, @Param(value = "examRoomID") String examRoomID);

    @SelectProvider(type = ExamRoomMapperProvider.class,method = "findExamRoomListBySomeWhere")
    List<ExamRoom> findExamRoomListBySomeWhere(@Param(value = "appId") String appId, @Param(value = "planId") String planId, @Param(value = "sceneId") String sceneId, @Param(value = "subjectId") String subjectId);

    @Insert("INSERT INTO ExamRoom (\n" +
            "\tAppID,ExamSiteID,ExamPlanInfoID,ExamRoomID,ExamRoomCode,ExamRoomName,MachineNum,BakMachineNum\n" +
            ") SELECT #{appID},#{examSiteID},#{examPlanInfoID},#{examRoomID},#{examRoomCode},#{examRoomName},#{machineNum},#{bakMachineNum}\n" +
            "FROM\n" +
            "\tDUAL\n" +
            "WHERE\n" +
            "\tNOT EXISTS (\n" +
            "\t\tSELECT\n" +
            "\t\t\t1\n" +
            "\t\tFROM\n" +
            "\t\t\tExamRoom\n" +
            "\t\tWHERE\n" +
            "\t\t\tExamSiteID = #{examSiteID}\n" +
            "\t\tAND ExamRoomID = #{examRoomID}\n" +
            "\t\tLIMIT 1\n" +
            ")")
    Integer insertExamRoom(ExamRoom examRoom);

    @Update("UPDATE ExamRoom SET ExamRoomCode = #{examRoomCode},ExamRoomName = #{examRoomName},MachineNum = #{machineNum},BakMachineNum = #{bakMachineNum} WHERE AppID = #{appID} AND ExamSiteID = #{examSiteID} AND ExamRoomID = #{examRoomID}")
    Integer updateExamRoom(ExamRoom examRoom);
}
